﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using ListViewDragDrop.ServiceProviders.UI;
using TeacherLeftHand.Model;
using TeacherLeftHand.ViewModel;

namespace TeacherLeftHand.View
{
    /// <summary>
    /// Description for KnowledgeItemListView.
    /// </summary>
    public partial class KnowledgeItemListView
    {


        public KnowledgeItemListView()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(KnowledgeItemListView_Loaded);

            
        }


        public void KnowledgeItemListView_Loaded(object sender, RoutedEventArgs e)
        {
            #region Register DragManager
            var dragHelperListBox0 = new ListViewDragManager<KnowledgeItem>(this.listKnowledgeItem);
            #endregion
            //// Data Provider
            // var listBoxDataProvider = new ListBoxDataProvider<ListBox, ListBoxItem>("ListBoxItemObject", false);
            // DragManager
            // var dragHelperListBox0 = new DragManager(this.listKnowledgeItem, listBoxDataProvider);

            // DragManager
            //var dragHelperCanvas0 = new DragManager(this.gridPanelKI,
            //     new IDataProvider[] {
            //        panelDataProvider,
            //    });
            // var dragdrop = new ListViewDragDropManager<KnowledgeItem>(this.listKnowledgeItem);
            //var listOfTMAction = new ObservableCollection<KnowledgeItem>();
            //var listBoxDataProvider = new ListBoxDataProvider<ListBox, ListBoxItem>("ListBoxItemObject", false);


            //var knowledgeItems = new ObservableCollection<KnowledgeItem>();
            //var itemcount = this.listKnowledgeItem.Items.Count;
            //totalPage = itemcount / itemPerPage;
            //if (itemcount % itemPerPage != 0)
            //{
            //    totalPage += 1;
            //}

            //foreach (var knowledgeItem in listKnowledgeItem.Items)
            //{
            //   knowledgeItems.Add((KnowledgeItem)knowledgeItem);
            //}

            //view.Filter += new FilterEventHandler(view_Filter);
            //this.listKnowledgeItem.DataContext = view;
            //ShowCurrentPageIndex();
            //this.tbTotalPage.Text = totalPage.ToString();
            ////ListProducts();

        }
        //void view_Filter(object sender, FilterEventArgs e)
        //{
        //    int index = this.listKnowledgeItem.Items.IndexOf((KnowledgeItem)e.Item);

        //    if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
        //    {
        //        e.Accepted = true;
        //    }
        //    else
        //    {
        //        e.Accepted = false;
        //    }
        //}
        //private void ShowCurrentPageIndex()
        //{
        //    this.tbPageNumber.Text = (currentPageIndex + 1).ToString();
        //}
        //private void btnFirst_Click(object sender, RoutedEventArgs e)
        //{
        //    // Display the first page
        //    if (currentPageIndex != 0)
        //    {
        //        currentPageIndex = 0;
        //        view.View.Refresh();
        //    }
        //    ShowCurrentPageIndex();
        //}
        //private void btnPrev_Click(object sender, RoutedEventArgs e)
        //{
        //    // Display previous page
        //    if (currentPageIndex > 0)
        //    {
        //        currentPageIndex--;
        //        view.View.Refresh();
        //    }
        //    ShowCurrentPageIndex();
        //}
        //private void btnNext_Click(object sender, RoutedEventArgs e)
        //{
        //    // Display next page
        //    if (currentPageIndex < totalPage - 1)
        //    {
        //        currentPageIndex++;
        //        view.View.Refresh();
        //    }
        //    ShowCurrentPageIndex();
        //}
        //private void btnLast_Click(object sender, RoutedEventArgs e)
        //{
        //    // Display the last page
        //    if (currentPageIndex != totalPage - 1)
        //    {
        //        currentPageIndex = totalPage - 1;
        //        view.View.Refresh();
        //    }
        //    ShowCurrentPageIndex();
        //}
        //private void btnNext_Click(object sender, System.EventArgs e)
        //{
        //    CustomPaging((int)PagingMode.Next);
        //}

        //private void btnPrev_Click(object sender, System.EventArgs e)
        //{
        //    CustomPaging((int)PagingMode.Previous);
        //}

        //private void btnFirst_Click(object sender, System.EventArgs e)
        //{
        //    CustomPaging((int)PagingMode.First);
        //}

        //private void btnLast_Click(object sender, System.EventArgs e)
        //{
        //    CustomPaging((int)PagingMode.Last);
        //}



        //#region Private Functions

        //private void CustomPaging(int mode)
        //{
        //    //int totalRecords = dt_Products.Rows.Count;
        //    //int pageSize = paging_NoOfRecPerPage;
        //    //int currentPageIndex = paging_PageIndex;

        //    if (dt_Products.Rows.Count <= paging_NoOfRecPerPage)
        //    {
        //        return;
        //    }

        //    switch (mode)
        //    {
        //        case (int)PagingMode.Next:
        //            if (dt_Products.Rows.Count > (paging_PageIndex * paging_NoOfRecPerPage))
        //            {
        //                DataTable tmpTable = new DataTable();
        //                tmpTable = dt_Products.Clone();

        //                if (dt_Products.Rows.Count >= ((paging_PageIndex * paging_NoOfRecPerPage) + paging_NoOfRecPerPage))
        //                {
        //                    for (int i = paging_PageIndex * paging_NoOfRecPerPage; i < ((paging_PageIndex * paging_NoOfRecPerPage) + paging_NoOfRecPerPage); i++)
        //                    {
        //                        tmpTable.ImportRow(dt_Products.Rows[i]);
        //                    }
        //                }
        //                else
        //                {
        //                    for (int i = paging_PageIndex * paging_NoOfRecPerPage; i < dt_Products.Rows.Count; i++)
        //                    {
        //                        tmpTable.ImportRow(dt_Products.Rows[i]);
        //                    }
        //                }

        //                paging_PageIndex += 1;

        //                listKnowledgeItem.ItemsSource = tmpTable.DefaultView;

        //                 tmpTable.Dispose();
        //            }
        //            break;
        //        case (int)PagingMode.Previous:
        //            if (paging_PageIndex > 1)
        //            {
        //                DataTable tmpTable = new DataTable();
        //                tmpTable = dt_Products.Clone();

        //                paging_PageIndex -= 1;

        //                for (int i = ((paging_PageIndex * paging_NoOfRecPerPage) - paging_NoOfRecPerPage); i < (paging_PageIndex * paging_NoOfRecPerPage); i++)
        //                {
        //                    tmpTable.ImportRow(dt_Products.Rows[i]);
        //                }

        //                listKnowledgeItem.ItemsSource = tmpTable.DefaultView;
        //                tmpTable.Dispose();
        //            }
        //            break;
        //        case (int)PagingMode.First:
        //            paging_PageIndex = 2;
        //            CustomPaging((int)PagingMode.Previous);
        //            break;
        //        case (int)PagingMode.Last:
        //            paging_PageIndex = (dt_Products.Rows.Count / paging_NoOfRecPerPage)-1;
        //            CustomPaging((int)PagingMode.Next);
        //            break;
        //    }

        //    //DisplayPagingInfo();
        //}

        ////private void DisplayPagingInfo()
        ////{
        ////    string pagingInfo = "Displaying " + (((paging_PageIndex - 1) * paging_NoOfRecPerPage) + 1) + " to " + paging_PageIndex * paging_NoOfRecPerPage;

        ////    if (dt_Products.Rows.Count < (paging_PageIndex * paging_NoOfRecPerPage))
        ////    {
        ////        pagingInfo = "Displaying " + (((paging_PageIndex - 1) * paging_NoOfRecPerPage) + 1) + " to " + dt_Products.Rows.Count;
        ////    }
        ////    lblPagingInfo.Content = pagingInfo;

        ////    lblPageNumber.Content = paging_PageIndex;
        ////}

        ////private void MoveWindow()
        ////{
        ////    ReleaseCapture();
        ////    //SendMessage(new WindowInteropHelper(main).Handle, mouseDown, Caption, 0);
        ////}

        //private void ListProducts()
        //{
        //    try
        //    {
        //        paging_PageIndex = 1;
        //        dt_Products.Rows.Clear();
        //        dt_Products = ToDataTable<KnowledgeItem>(listKnowledgeItem.Items);

        //        if (dt_Products.Rows.Count > 0)
        //        {
        //            var tmpTable = new DataTable();
        //            tmpTable = dt_Products.Clone();

        //            if (dt_Products.Rows.Count >= paging_NoOfRecPerPage)
        //            {
        //                for (int i = 0; i < paging_NoOfRecPerPage; i++)
        //                {
        //                    tmpTable.ImportRow(dt_Products.Rows[i]);
        //                }
        //            }
        //            else
        //            {
        //                for (int i = 0; i < dt_Products.Rows.Count; i++)
        //                {
        //                    tmpTable.ImportRow(dt_Products.Rows[i]);
        //                }
        //            }

        //            listKnowledgeItem.ItemsSource = tmpTable.DefaultView;
        //            tmpTable.Dispose();

        //            //DisplayPagingInfo();
        //        }
        //        else
        //        {
        //            MessageBox.Show("No Records Exists for the selected category");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //}

        //// remove "this" if not on C# 3.0 / .NET 3.5
        //public  DataTable ToDataTable<T>( ItemCollection data)
        //{
        //    PropertyDescriptorCollection props =
        //        TypeDescriptor.GetProperties(typeof(T));
        //    DataTable table = new DataTable();
        //    for (int i = 0; i < props.Count; i++)
        //    {
        //        PropertyDescriptor prop = props[i];
        //        table.Columns.Add(prop.Name, prop.PropertyType);
        //    }
        //    object[] values = new object[props.Count];
        //    foreach (T item in data)
        //    {
        //        for (int i = 0; i < values.Length; i++)
        //        {
        //            values[i] = props[i].GetValue(item);
        //        }
        //        table.Rows.Add(values);
        //    }
        //    return table;
        //}

        //#endregion
    }
}